package com.example.com.skills_utf8;

import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.app.BaseFragment;
import com.app.MyCamera;
import com.app.util.Utils_Log;
import com.app.video.MyVideoCamera;
import com.app.video.MyVideoCameraPreview;

import java.util.Timer;
import java.util.TimerTask;


//
public class _64_3Fragment extends Fragment {


    private final String TAG = _64_3Fragment.class.getSimpleName();


    MyVideoCameraPreview preview;
    Timer timer = new Timer();

    Handler handler = new Handler();


    // 手势监听
    GestureDetectorCompat gesturedetector ;
    MyGestureListener gestureListener = new MyGestureListener();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        final View view = inflater.inflate(R.layout.fragment_64_3, container, false);


        // 生成手势监听对象；
        gesturedetector = new GestureDetectorCompat(getActivity(), gestureListener);


        final FrameLayout layout_camera_content = (FrameLayout)view.findViewById(R.id.layout_camera_content);

        preview = new MyVideoCameraPreview();
        final View iv_camera_content = (View)preview.init(getActivity());

        layout_camera_content.addView(iv_camera_content);

        preview.start();

        //
        iv_camera_content.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                float x = event.getX();  //相对于本身view的左上角的偏移值
                float y = event.getY();


                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Utils_Log.e(TAG, "x: " + x + " y: " + y);
                    preview.SetAutoFoucs();


                    // 在屏幕绘制聚焦图案
                    int width = iv_camera_content.getWidth();
                    int height = iv_camera_content.getHeight();

                    int width_box = 256;
                    int height_box = 256;

                    // 本身就在屏幕坐标系下,无需转换
                    int pos_x = (int) x; //width/2;
                    int pos_y = (int) y; //height/2;


                    //
                    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    Paint paint = new Paint();
                    Rect rect = new Rect();
                    rect.left = pos_x - width_box / 2;
                    rect.top = pos_y - height_box / 2;
                    rect.right = rect.left + width_box;
                    rect.bottom = rect.top + height_box;


                    paint.setColor(Color.BLUE);
                    paint.setStyle(Paint.Style.STROKE);
                    paint.setStrokeWidth(5);
                    canvas.drawRect(rect, paint);



                    /*
                    if(layout_camera_content.getChildCount() > 1){
                        for(int i =0; i<layout_camera_content.getChildCount(); i++){
                            View view = layout_camera_content.getChildAt(i);
                            if(view instanceof ImageView){
                                layout_camera_content.removeView(view);
                            }
                        }
                    }
                    ImageView iv = new ImageView(getActivity());
                    iv.setImageBitmap(bitmap);

                    layout_camera_content.addView(iv);
                    */

                    final ImageView iv = (ImageView) view.findViewById(R.id.iv_frame_box);
                    iv.setImageBitmap(bitmap);


                    // 播放动画， 播放两组动画
                    Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.scale_and_alpha);
                    animation.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            // 淡入，颜色加深
                            //Animation animation_fadein = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
                            //iv.startAnimation(animation_fadein);

                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                    iv.startAnimation(animation);


                }


                return gesturedetector.onTouchEvent(event);
            }
        });




        preview.mycamera.printSupportFocusMode();
        preview.mycamera.printSupportFlashMode();
        preview.mycamera.printZoomRatios();;


        int MaxZoom = preview.mycamera.GetMaxZoom();
        SeekBar sb_zoom = (SeekBar)view.findViewById(R.id.sb_zoom);
        sb_zoom.setMax(MaxZoom);
        sb_zoom.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                preview.mycamera.SetZoom(progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        //ProgressBar bar = new ProgressBar(getActivity());




        return view;

    }


    @Override
    public void onPause() {
        super.onPause();
        //System.out.println("onPause");

        preview.close();
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //System.out.println("onDestroy");

        //preview.close();
    }



    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Utils_Log.e(TAG, "onDoubleTap");

            // 使相机变焦，拉近
            if(preview.mycamera.GetZoom() == preview.mycamera.GetMaxZoom() ){

                new Thread(new Runnable() {

                    int progress = preview.mycamera.GetMaxZoom();

                    public void run() {
                        while (progress > 0) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }

                            // Update the progress bar
                            handler.post(new Runnable() {
                                public void run() {
                                    preview.mycamera.SetZoom(progress--);
                                }
                            });
                        }
                    }
                }).start();

            }
            else{

                new Thread(new Runnable() {

                    int MaxZoom = preview.mycamera.GetMaxZoom();
                    int progress = 0;

                    public void run() {
                        while (progress < MaxZoom) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }

                            // Update the progress bar
                            handler.post(new Runnable() {
                                public void run() {
                                    preview.mycamera.SetZoom(progress++);
                                }
                            });
                        }
                    }
                }).start();

            }




            return super.onDoubleTap(e);
        }


        @Override
        public boolean onDown(MotionEvent e) {
            Utils_Log.e(TAG, "onDown");
            return true;        // 返回true，使后续能接着处理
        }

    }





}
